const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderInfo:{},
    inPay:false,
    theid:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.getList(options.id)
    this.setData({
      theid: options.id
    })
  },

  getList(theId){
    var that = this
    let sign = wx.getStorageSync('sign')
    wx.request({
      url: app.urlData.getURL + "/index.php/Weshop/SApi/index/m/orderDetail/key/" + app.urlData.key + "/wid/" + app.urlData.wid + "/sign/" + sign,
      method: 'get',
      data: {
        order_id: theId,
      },
      success: (res) => {
        console.log(res)
        that.setData({
          orderInfo: res.data.datas
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  wxplay(e) {
    var that = this
    if(wx.getStorageSync('customerid') == '' || wx.getStorageSync('customerid') == 0){
      wx.showModal({
        title: '提示',
        content: '您还没有注册，是否去注册',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/phoneBinding/phoneBinding',
            })
          }
        }
      })
    }else{
      if (that.data.inPay == false) {
        that.setData({
          inPay: true
        })
        wx.request({
          url: app.urlData.getURL + "/index.php/Weshop/SApi/index/m/saveOrderByOrderId/key/" + app.urlData.key + "/wid/" + app.urlData.wid + "/sign/" + wx.getStorageSync("sign"),
          method: 'get',
          data: {
            order_id: that.data.orderInfo.order.order_id,
            vip_id: wx.getStorageSync('vip_id')
          },
          success: (res) => {
            if (res.data.status == -1) {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: res.data.msg
              })
            } else {
              wx.requestPayment({
                'appId': res.data.datas.appId,
                'nonceStr': res.data.datas.nonceStr,
                'package': res.data.datas.package,
                'signType': res.data.datas.signType,
                'paySign': res.data.datas.paySign,
                'timeStamp': res.data.datas.timeStamp,
                'success': function (res) {
                  wx.showModal({
                    title: '恭喜您',
                    showCancel: false,
                    content: '已成功购买，请在系统消息中查看！',
                    success: function (res) {
                      //这里写支付成功后操作
                      //wx.navigateBack();
                      that.getList(that.data.theid)

                    }
                  })
                }
              })
            }
            that.setData({
              inPay: false
            })
          }
        })
      }
    }
   
  },
  getGiftCode:function(e){


    let current = e.currentTarget.dataset.src;
      wx.previewImage({
        current: current, // 当前显示图片的http链接  
        urls: [current] // 需要预览的图片http链接列表  
      })
       
  },
  deleOrder: function (e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否确定取消订单？",
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.urlData.getURL + "/index.php/Weshop/SApi/index/m/cancelOrder/key/" + app.urlData.key + "/wid/" + app.urlData.wid + "/sign/" + wx.getStorageSync("sign"),
            method: 'get',
            data: {
              order_id: that.data.orderInfo.order.order_id
            },
            success: (res) => {
              if (res.data.status == 1) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'success',
                  duration: 800
                });
                setTimeout(function () {
                  wx.navigateBack()
                }, 800)
              }
            }
          })
        }
      }
    });
  },
})